<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateLinksTable extends Migration
{
    /**
     * Run the migrations.
     * @return void
     */
    public function up()
    {
        Schema::create('links', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 50)->default('')->comment('名称');
            $table->string('url', 100)->default('')->comment('链接');
            $table->string('pic1', 100)->default('')->comment('图片');
            $table->tinyInteger('status')->default(0)->comment('状态');
            $table->dateTime('created_at')->nullable()->comment('创建时间');
            $table->dateTime('updated_at')->nullable()->comment('更新时间');
        });
        Schema::create('linktags', function (Blueprint $table) {
            $table->increments('id');
            $table->string('tag', 50)->default('')->comment('标签');
        });
        Schema::create('link_tag', function (Blueprint $table) {
            $table->integer('link_id')->unsigned()->comment('链接Id');
            $table->integer('tag_id')->unsigned()->comment('标签Id');
            $table->primary(['link_id', 'tag_id']);
            $table->foreign('link_id')->references('id')->on('links')->onDelete('cascade');
            $table->foreign('tag_id')->references('id')->on('linktags')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('link_tag');
        Schema::dropIfExists('links');
        Schema::dropIfExists('linktags');
    }
}
